package com.cxps.server.handler;

import com.alibaba.fastjson.JSON;
import com.cxps.cloud.message.ApiResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 未登录处理类
 *
 * @author YouCai.Liu
 * @since 2021/10/24
 * ...
 */
public class AuthNoLoginHandler implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException {
        System.out.println("No Login： " + exception.getMessage());
        ApiResponse<Authentication> apiResponse = new ApiResponse<>();
        apiResponse.setCode("2200");
        apiResponse.setMsg("未登录");
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(apiResponse));
    }
}